import 'package:custom_widgets/item/item_normal_widget.dart';
import 'package:flutter/material.dart';
import 'package:mi_mang_flutter/api/user_api.dart';
import 'package:mi_mang_flutter/manager/app_manager.dart';
import 'package:mi_mang_flutter/page/setting/setting_router.dart';
import 'package:mi_mang_flutter/provider/user_provider.dart';
import 'package:mi_mang_flutter/res/dimens.dart';
import 'package:mi_mang_flutter/res/styles.dart';
import 'package:mi_mang_flutter/routers/fluro_navigator.dart';
import 'package:mi_mang_flutter/utils/dialog_util.dart';
import 'package:mi_mang_flutter/utils/locale_util.dart';
import 'package:provider/provider.dart';

/// 账号与安全
class AccountPage extends StatefulWidget {
  AccountPage({Key? key}) : super(key: key);

  @override
  _AccountPageState createState() => _AccountPageState();
}

class _AccountPageState extends State<AccountPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(i18n(context).accountSecurity),
        centerTitle: true,
      ),
      body: _buildBody(),
    );
  }

  Widget _buildBody() {
    return Consumer<UserProvider>(builder: (context, provider, _) {
      return ListView(
        padding: EdgeInsets.symmetric(vertical: Dimens.gapDp10),
        children: <Widget>[
          ItemNormalWidget.item(
            title: i18n(context).account,
            subtitle: provider.user?.username,
          ),
          Gaps.vGap10,
          ...ItemNormalWidget.divideTiles(childs: <Widget>[
            ItemNormalWidget.item(
              title: i18n(context).email,
              subtitle: provider.user?.email ?? i18n(context).goToBind,
              onTap: provider.user?.email == null
                  ? () {
                      NavigatorUtils.push(
                          context, SettingRouter.accountBindEmail);
                    }
                  : null,
            ),
            ItemNormalWidget.item(
              title: i18n(context).updatePassword,
              onTap: () {
                NavigatorUtils.push(context, SettingRouter.updatePassword);
              },
            ),
            // ItemNormalWidget.item(
            //   title: i18n(context).gestureLogin,
            //   onTap: () {},
            // ),
          ]),
          Gaps.vGap10,
          // ItemNormalWidget.item(
          //   title: i18n(context).thirdlyLogin,
          //   onTap: () {},
          // ),
          // Gaps.vGap10,
          // Padding(
          //   padding: EdgeInsets.symmetric(
          //     horizontal: Dimens.gapDp15,
          //     vertical: Dimens.gapDp15,
          //   ),
          //   child: OutlineButton.icon(
          //     icon: Icon(Icons.power_settings_new),
          //     shape: StadiumBorder(),
          //     label: Container(
          //       alignment: Alignment.center,
          //       child: Text(i18n(context).logoutAccount),
          //     ),
          //     onPressed: () {},
          //   ),
          // ),
          Padding(
            padding: EdgeInsets.symmetric(
              horizontal: Dimens.gapDp15,
              vertical: Dimens.gapDp15,
            ),
            child: TextButton.icon(
              icon: Icon(Icons.power_settings_new),
              style: TextButton.styleFrom(
                shape: StadiumBorder(
                  side: BorderSide(color: Theme.of(context).dividerColor),
                ),
                textStyle: Theme.of(context).textTheme.labelLarge!,
              ).copyWith(
                overlayColor:
                    WidgetStateProperty.all(Theme.of(context).splashColor),
              ),
              label: Text(i18n(context).logoutAccount),
              onPressed: () {
                DialogUtil.showDialogMsg(
                  context,
                  msg: i18n(context).logoutAccountDescribe,
                  onPressed: () {
                    UserApi.clearAccount().then((value) {
                      AppManager.logOut(context);
                      AppManager.goBackHomeTabPage(context);
                    });
                  },
                );
              },
            ),
          ),
        ],
      );
    });
  }
}
